<?php
namespace Home\Controller;
use Common\Controller\FirstendController;

class ArticleController extends FirstendController {
	
    public function _initialize()
    {
        parent::_initialize();
        $this->cats_mod = D('ArticleCate');
        $this->_mod = M('Article');
        $this->cid = $_SERVER['HTTP_HOST'];
        $this->_cate_mod = D('ItemsCate');
        $this->assign('nav_curr', 'index');
        $this->article_mod = M('Article');
        $this->cats_list = $this->cats_mod->get_cats();
        $this->assign('acats', $this->cats_list);
    }
    public function index()
    {
        $cid = I('id', '', 'trim');
        $p = I('p', 1, 'intval');
        $map['status'] = '1';
        if ($cid) {
            $map['cate_id'] = $cid;
            $title = $this->cats_list[$cid]['name'];
        }
        $page_size = 20;
        $start = $page_size * ($p - 1);
        $order = 'ordid asc ';
        $order .= ', id DESC';
        $cat = $this->cats_mod->find($cid);
        $article_list = $this->article_mod->where($map)->order($order)->limit($start . ',' . $page_size)->select();
        foreach ($article_list as $key => $val) {
            $article_list[$key] = $val;
            $data = $val['info'];
            $data = preg_replace('@<script(.*?)</script>@is', '', $data);
            $data = preg_replace('@<iframe(.*?)</iframe>@is', '', $data);
            $data = preg_replace('@<style(.*?)</style>@is', '', $data);
            $data = preg_replace('@<(.*?)>@is', '', $data);
            $data = str_replace('&nbsp;', '', $data);
            $data = mb_substr($data, 0, 380, 'utf-8');
            $article_list[$key]['info'] = $data;
            $img = $val['info'];
            preg_match('<img.*src=["](.*?)["].*?>', $img, $match);
        }
        $this->assign('cid', $cid);
        $this->assign('article_list', $article_list);
        $count = $this->article_mod->where($map)->count();
        $pager = $this->_pager($count, $page_size);
        $this->assign('page', $pager->jshow());
        $this->assign('nav_curr', 'article');
        $this->_config_seo(C('itk_seo_config.article_index'));
        $this->display('index');
    }
    public function cate()
    {
        $cid = I('id', '', 'trim');
        $item = $this->cats_mod->where(array('id' => $cid))->find();
        !$item && $this->_404();
        $p = I('p', 1, 'intval');
        $map['status'] = '1';
        if ($cid) {
            $map['cate_id'] = $cid;
            $title = $this->cats_list[$cid]['name'];
        }
        $page_size = 10;
        $start = $page_size * ($p - 1);
        $order = 'ordid asc ';
        $order .= ', id DESC';
        $cat = $this->cats_mod->find($cid);
        $article_list = $this->article_mod->where($map)->order($order)->limit($start . ',' . $page_size)->select();
        $this->assign('cid', $cid);
        $this->assign('cat', $cat);
        $this->assign('article_list', $article_list);
        $count = $this->article_mod->where($map)->count();
        $pager = $this->_pager($count, $page_size);
        $this->assign('page', $pager->nshow());
        $this->assign('cpage', $pager->cshow());
        $this->assign('nav_curr', 'article');
        $this->_config_seo(C('itk_seo_config.article_cate'), array('title' => $cat['name'], 'cate_name' => $cat['name'], 'seo_title' => $cat['seo_title'], 'seo_keywords' => $cat['seo_keys'], 'seo_description' => $cat['seo_desc']));
        $this->display('cate');
    }
    public function tag()
    {
        $k = I('k');
        $k = urldecode($k);
        if ($k) {
            if (strpos($k, ' ')) {
                $karr = split(' ', $k);
                foreach ($karr as $kw) {
                    $like[] = array('like', '%' . $kw . '%');
                }
                $where['title'] = $like;
            } else {
                $where['title'] = array('like', '%' . $k . '%');
            }
            $this->assign('k', $k);
        }
        $where['status'] = '1';
        $p = I('p', 1, 'intval');
        $page_size = 20;
        $start = $page_size * ($p - 1);
        $order = 'ordid asc ';
        $order .= ', id DESC';
        $article_list = $this->article_mod->where($where)->order($order)->limit($start . ',' . $page_size)->select();
        $this->assign('article_list', $article_list);
        $count = $this->article_mod->where($where)->count();
        $pager = $this->_pager($count, $page_size);
        $this->assign('page', $pager->nshow());
        $this->assign('cpage', $pager->cshow());
        $this->assign('nav_curr', 'article');
        $this->assign('count', $count);
        $page_seo = array('title' => '搜索"' . $k . '"相关的文章 - 第' . $p . '页 - ' . C('itk_site_name'), 'keywords' => $k, 'description' => '搜索到' . $count . '篇与' . $k . '相关的文章。');
        $this->assign('page_seo', $page_seo);
        $this->display('tag');
    }
    public function read()
    {
    	
    	$id = I('id', '', 'intval');
    	$tron = $this->_mod->where(array('id' => $id))->find();
    	!$tron && $this->_404();
    	$fzurl = C('itk_site_url') . U('Article/read', array('id' => $tron['id']));
    	$cid = $tron['cate_id'];

    	$article_mod = M('Article');
    	$_ar = D('Article');
    	$where['id'] = array('LT',$id);
    	$articlep = $article_mod->where($where)->order("id desc")->find();			// 前一篇文章
    	$where['id'] = array('GT',$id);
    	$articlen = $article_mod->where($where)->order("id desc")->find();
    	
    	$_ar->hits($id);
    	if ($cid) {
    		$map['cate_id'] = $cid;
    		$item = M('ArticleCate')->field('id,name')->find($cid);
    	}

    	$this->cate_list = D('ItemsCate')->field('id,name,pid')->select();
    	$article = $article_mod->field('id,cate_id,title,tags,mpic,info,hits,author,seo_title,seo_keys,seo_desc,add_time,status')->find($id);
    	
    	$tag_list = $_ar->get_tags_by_title($article['title']);
    	$tags = implode(',', $tag_list);
    	foreach ($tag_list as $nkeys) {
    		if (strpos($tron['info'], $nkeys)) {
    			$ekeys = urlencode($nkeys);
    			$url = U('Article/tag', array('k' => $ekeys));
    			$tron['info'] = preg_replace('/' . $nkeys . '/', '<a style=\'top:0px;\' href=' . $url . ' target=_blank ><strong style=\'color:red;top:0px;\'>' . $nkeys . '</strong></a>', $tron['info'], 1);
    		}
    	}
    	$this->assign('tags', $tag_list);
    	$description = $article['info'];
    	$description = preg_replace('@<script(.*?)</script>@is', '', $description);
    	$description = preg_replace('@<iframe(.*?)</iframe>@is', '', $description);
    	$description = preg_replace('@<style(.*?)</style>@is', '', $description);
    	$description = preg_replace('@<(.*?)>@is', '', $description);
    	$description = str_replace('&nbsp;', '', $description);
    	$description = mb_substr($description, 0, 150, 'utf-8');
    	$this->assign('description', $description);
    	if (!$article['seo_desc']) {
    		$seo_desc = $description;
    	} else {
    		$seo_desc = $article['seo_desc'];
    	}
    	$this->_config_seo(C('itk_seo_config.article'), array('title' => $article['title'], 'seo_title' => $article['seo_title'], 'seo_keywords' => $article['tags'], 'seo_description' => $seo_desc));
    	$test = $article['info'];
    	preg_match('<img.*src=["](.*?)["].*?>', $test, $match);
    	$picurls = $match[1];
    	$this->assign('tags', $tag_list);
    	$this->assign('picurls', $picurls);
    	$this->assign('cid', $cid);
    	$this->assign('article_cate_list', $article_cate_list);
    	$this->assign('nid', $nid);
    	$this->assign('preid', $preid);
    	$this->assign('lastid', $lastid);
    	$this->assign('item', $item);
    	$tron['info'] = htmlspecialchars_decode($tron['info']);
    	$this->assign('tron', $tron);
    	$this->assign('fzurl', $fzurl);
    	$this->assign('hits', $hits);
    	$this->assign('nav_curr', 'article');
    	$this->assign('articlep', $articlep);
    	$this->assign('articlen', $articlen);
    	$this->assign('article', $article);
    	$this->display();
    
    }
}